<?php

namespace App\Repository\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;

/**
 * Class CompanyMaterial.
 *
 * @package namespace App\Repository\Models;
 */
class CompanyMaterial extends Model implements Transformable
{
    use TransformableTrait;
    use SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $table = 'company_material';


    protected $fillable = ['id','user_id','company_id','ent_type','ent_property','ent_emp_number','ent_logo','ent_service_tag','ent_idea','ent_intro','contact_person','contact_sex','contact_position','contact_mobile','contact_tel','contact_fax','contact_address','contact_postcode','business_address','business_trade','business_product','customer_groups','sales_region','business_sum','purchase_products','purchase_region','url','created_at','updated_at','business_model','deleted_at'];

    protected $appends = ['ent_type_name','ent_property_name','ent_service_tag_name','business_product_name','customer_groups_name','purchase_products_name','business_trade_name','contact_address_name','business_address_name','sales_region_name','purchase_region_name','business_sum','list_address_name'];
    //公司性质
    public static $ent_property_map = [
        1 => '国有企业',
        2 => '集体企业',
        3 => '有限责任公司',
        4 => '股份有限公司',
        5 => '联营企业',
        6 => '股份合作公司',
        7 => '私营企业',
        8 => '港澳台商投资企业',
        9 => '外商投资企业',
        10 => '其他',

    ];

    public function getEntPropertyNameAttribute()
    {
        if (isset($this->attributes['ent_property'])) {
            return array_get(self::$ent_property_map, $this->attributes['ent_property']) ?: '-';
        }
        return null;
    }

    public function getEntTypeNameAttribute()
    {
        if (isset($this->attributes['ent_type'])) {
            $value = json_decode($this->attributes['ent_type'], TRUE);
            $data = '';
            if (is_array($value)) {
                $data = implode(',', $value);
            }
            return $data;
        }
        return null;
    }

    public function getEntServiceTagNameAttribute()
    {
        if (isset($this->attributes['ent_service_tag'])) {
            $value = json_decode($this->attributes['ent_service_tag'], TRUE);
            $data = '';
            if (is_array($value)) {
                $data = implode(',', $value);
            }
            return $data;
        }
        return null;
    }

    public function getBusinessProductNameAttribute()
    {
        if (isset($this->attributes['business_product'])) {
            $value = json_decode($this->attributes['business_product'], TRUE);
            $data = '';
            if (is_array($value)) {
                $data = implode(',', $value);
            }
            return $data;
        }
        return null;
    }

    public function getBusinessTradeNameAttribute()
    {
        if (isset($this->attributes['business_trade'])) {
            $value = json_decode($this->attributes['business_trade'], TRUE);
            $data = '';
            if (is_array($value)) {
                $data = implode(',', $value);
            }
            return $data;
        }
        return null;
    }

    public function getCustomerGroupsNameAttribute()
    {
        if (isset($this->attributes['customer_groups'])) {
            $value = json_decode($this->attributes['customer_groups'], TRUE);
            $data = '';
            if (is_array($value)) {
                $data = implode(',', $value);
            }
            return $data;
        }
        return null;
    }

    public function getPurchaseProductsNameAttribute()
    {
        if (isset($this->attributes['purchase_products'])) {
            $value = json_decode($this->attributes['purchase_products'], TRUE);
            $data = '';
            if (is_array($value)) {
                $data = implode(',', $value);
            }
            return $data;
        }
        return null;
    }

    public function getSalesRegionNameAttribute()
    {
        if (isset($this->attributes['sales_region'])) {
            $value = json_decode($this->attributes['sales_region'], TRUE);
            $data = '';
            if (is_array($value)) {
                $data = implode(',', $value);
            }
            return $data;
        }
        return null;
    }

    public function getPurchaseRegionNameAttribute()
    {
        if (isset($this->attributes['purchase_region'])) {
            $value = json_decode($this->attributes['purchase_region'], TRUE);
            $data = '';
            if (is_array($value)) {
                $data = implode(',', $value);
            }
            return $data;
        }
        return null;
    }



    public function getContactAddressNameAttribute()
    {
        if (isset($this->attributes['contact_address'])) {
            $value = unserialize($this->attributes['contact_address']);
            $data = '';
            if (is_array($value)) {
                $data = implode('--', $value);
            }
            return $data;
        }
        return null;
    }

    public function getBusinessAddressNameAttribute()
    {
        if (isset($this->attributes['business_address'])) {
            $value = unserialize($this->attributes['business_address']);
            $data = '';
            if (is_array($value)) {
                $data = implode('--', $value);
            }
            return $data;
        }
        return null;
    }

    public function getBusinessSumAttribute()
    {
        if (isset($this->attributes['business_sum'])) {
            $value = unserialize($this->attributes['business_sum']);
            $data = '';
            if (isset($value['amount'])) {
                $data = $value['amount']. ' ' .$value['unit'];
            }
            return $data;
        }
        return 0;
    }

    public function getListAddressNameAttribute()
    {
        if (isset($this->attributes['business_address'])) {
            $value = unserialize($this->attributes['business_address']);
            $data = $value['regions'];
            return $data;
        }
        return null;
    }

}
